<?php
/**
 * 商品信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-13
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/
class Goods extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	
	
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{goods}}';
	}
	
	/**
	 * 具体参数配置列
	 */
	public function getParamInfo($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT a.param_id,a.param_name,(SELECT b.param_name FROM {{param}} b WHERE a.parent_id=b.param_id ) as parent_name,(SELECT d.goods_param_id FROM {{goods_param}} d WHERE   a.param_id = d.param_id and d.active = 1 and d.goods_id = $goods_id) as goods_param_id  FROM {{param}} a WHERE a.parent_id <> 0 AND a.param_id IN(SELECT c.param_id FROM {{goods_param}} c)";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	
	/**
	 * sku配置列
	 */
	public function getStyleInfo($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT a.attr_id,a.attr_name,(SELECT b.attr_name FROM {{attr}} b WHERE a.parent_id=b.attr_id ) as parent_name,(SELECT d.goods_attr_id FROM {{goods_attr}} d WHERE   a.attr_id = d.attr_id and d.active = 1 and d.goods_id = $goods_id) as goods_attr_id  FROM {{attr}} a WHERE a.parent_id <> 0 AND a.attr_id IN(SELECT c.attr_id FROM {{goods_attr}} c)";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	
	/**
	 * 查找商品详细图
	 */
	public function getGoodsDetailsImg($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{images}} WHERE goods_id=".$goods_id;
		$result = $cmodel -> sel($_sql);
		return $result[0];
	}
	
	/**
	 * 搜索筛选内容
	 */
	
	public function searchContent($search,$brand_id = 0,$category_id = 0,$type_name){
		$cmodel = new CCCModel();
			$user = User::model();
			$user_name = Yii::app()->user->name;
			$user_name = "'$user_name'";
		$_sql = '';
		
		if($type_name == '管理员'){
			$_sql .= "SELECT * FROM {{goods}} WHERE 1=1 ";
		}elseif($type_name == '商家'){
			$_sql .= "SELECT * FROM {{goods}} WHERE goods_id IN(
				SELECT goods_id FROM {{goods_store}} WHERE store_id IN(
					SELECT store_id FROM {{store}} WHERE store_owner IN(
						SELECT user_id FROM {{user}} WHERE user_name=$user_name
					)
				)
			) AND 1=1 ";
		}
		$nextpage = "";
		if(isset($search['btquery']) && $search['btquery'] == '查询'){
			if($search['cat'] != -1){
				$nextpage .= "/category_id/".$search['cat'];
				$_sql .= " AND category_id=".$search['cat'];
			}
			if($search['brand'] != -1){
				$_sql .=" AND brand_id=".$search['brand'];
			}
			if($search['active'] != 3){
				$nextpage .= "/active/".$search['active'];
				$_sql .=" AND active=".$search['active'];
			}
			if($search['keyword'] != null){
				$nextpage .= "/keyword/".$search['keyword'];
				$_sql .= " AND (goods_name like '%".$search['keyword']."%' OR goods_info like '%".$search['keyword']."%' )";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
			if($brand_id != 0){
				$_sql .= " AND brand_id=".$brand_id;
			}
			if($category_id != 0){
				$_sql .= " AND category_id=".$category_id;
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
			/**
			 * 分页
			 */
			$per = 5;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY goods_sort ASC ".$page->limit;
			$result = $cmodel->sel($_sql);
			
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
		
		
		
		return $result;
	}
	
	
	
	
	/**
	 * 重新组装品牌信息
	 */
	public function getGoodsBrand(){
		$cmodel = new CCCModel();
		$_sql = "SELECT {{goods}}.brand_id,brand_name FROM {{brand}},{{goods}} WHERE {{brand}}.brand_id = {{goods}}.brand_id";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	public function getGoodsCategory(){
		$cmodel = new CCCModel();
		$_sql = "SELECT {{goods}}.category_id,{{category}}.category_name FROM {{goods}},{{category}} WHERE {{goods}}.category_id={{category}}.category_id";
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	
	
	
	
	/**
	 * 新增商品
	 */
	public function addGoods($addInfo){
		
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO 
					{{goods}}(
					`goods_name`,
					`goods_info`,
					`goods_salpro`,
					`goods_price`,
					`goods_zgprice`,
					`goods_tqprice`,
					`goods_thumbnail_url`,
					`category_id`,
					`brand_id`,
					`goods_birth`,
					`goods_keyword`,
					`goods_rest`,
					`goods_sell`,
					`goods_content`,
					`goods_producer`,
					`goods_supplier`,
					`goods_time`,
					`goods_update_time`,
					`goods_sort`,
					`active`
					) VALUES (
					'".$_POST['goods_name']."',
					'".$_POST['goods_info']."',
					'".$_POST['goods_salpro']."',
					'".$_POST['goods_price']."',
					'".$_POST['goods_zgprice']."',
					'".$_POST['goods_tqprice']."',
					'".$_POST['goods_thumbnail_url']."',
					'".$_POST['cat']."',
					'".$_POST['brand']."',
					'".$_POST['goods_birth']."',
					'".$_POST['goods_keyword']."',
					'".$_POST['goods_rest']."',
					'".$_POST['goods_sell']."',
					'".$_POST['goods_content']."',
					'".$_POST['goods_producer']."',
					'".$_POST['goods_supplier']."',
					NOW(),
					NOW(),
					'".$_POST['goods_sort']."',
					'".$_POST['active']."'
					)";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	
	/**
	 * 新增详细图
	 */
	public function addImages($addInfo,$id){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO
					{{images}}(
						`goods_id`,
						`img0`,
						`img1`,
						`img2`,
						`img3`,
						`img4`
						)
					VALUES(
						$id,
						'".$addInfo['picpic'][0]."',
						'".$addInfo['picpic'][1]."',
						'".$addInfo['picpic'][2]."',
						'".$addInfo['picpic'][3]."',
						'".$addInfo['picpic'][4]."'
					)";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 添加到店铺
	 */
	public function addToStore($addInfo,$id){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{goods_store}}
							(
								`store_id`,
								`goods_id`,
								`active`
							)
						VALUES(
								'".$addInfo['store_id']."',
								$id,
								0
						)";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 是否推荐
	 */
	public function rec($goods_id,$rec){
		$cmodel = new CCCModel();
		if($rec){
			$_sql = "UPDATE {{goods_store}} SET rec=0 WHERE goods_id=".$goods_id;			
		}else{
			$_sql = "UPDATE {{goods_store}} SET rec=1 WHERE goods_id=".$goods_id;
		}
		$cmodel->upd($_sql);
	}
	
	/**
	 * 修改所属店铺
	 */
	public function updateGoodsStore($addInfo,$id){
			$cmodel = new CCCModel();
			$_sqlInfo = "SELECT * FROM {{goods_store}} WHERE goods_id=".$id;
			$storeInfo = $cmodel -> sel($_sqlInfo);
			if(empty($storeInfo)){
				$_sql = "INSERT INTO {{goods_store}}(`store_id`,`goods_id`,`active`,`rec`)VALUES(".$addInfo['store_id'].",".$id.",0,0)";
			}
			else{
				$_sql = "UPDATE {{goods_store}}
							SET 	
								store_id = '".$addInfo['store_id']."'
							WHERE 
								goods_id = $id
						";
			}
			$row = $cmodel->upd($_sql);
			return $row;
		}
	
	/**
	 * 修改详细图
	 */
	public function updateImages($addInfo,$id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{images}}
						SET 	
							img0 = '".$addInfo[0]."',
							img1 = '".$addInfo[1]."',
							img2 = '".$addInfo[2]."',
							img3 = '".$addInfo[3]."',
							img4 = '".$addInfo[4]."'
						WHERE 
							goods_id = $id
					";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 修改商品信息
	 */
	public function updateGoods($info,$goods_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{goods}} SET 
					goods_name = '".$info['goods_name']."',
					goods_info = '".$info['goods_info']."',
					goods_price = '".$info['goods_price']."',
					goods_zgprice = '".$info['goods_zgprice']."',
					goods_tqprice = '".$info['goods_tqprice']."',
					goods_salpro = '".$info['goods_salpro']."',
					goods_thumbnail_url = '".$info['goods_thumbnail_url']."',
					category_id = '".$info['category_id']."',
					brand_id = '".$info['brand_id']."',
					goods_birth = '".$info['goods_birth']."',
					goods_keyword = '".$info['goods_keyword']."',
					goods_rest = '".$info['goods_rest']."',
					goods_sell = '".$info['goods_sell']."',
					goods_content = '".$info['goods_content']."',
					goods_producer = '".$info['goods_producer']."',
					goods_supplier = '".$info['goods_supplier']."',
					goods_time = '".@$info['goods_time']."',
					goods_update_time = NOW(),
					goods_sort = ".$info['goods_sort'].",
					onit = 0,
					active = 0
				 WHERE
				 	goods_id=$goods_id 					
		";
	
		$row = $cmodel->upd($_sql);
		return $row;
	}
	/**
	 * 删除商品信息
	 */
	public function deleteGoods($goods_id){
		$cmodel = new CCCModel();
		$_sql = "DELETE FROM {{goods}} WHERE goods_id=".$goods_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	/**
	 * 检测是否已经存在
	 */
	public function checkExist($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT * FROM {{goods}} where goods_name ='".$addInfo['goods_name']."'");
		return $result;
	}
	
	
	
	/**
	 * 查找单条记录
	 */
	public function getGoodsById($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{goods}} WHERE goods_id=".$goods_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	public function getCategoryByName($cat){
		$cmodel = new CCCModel();
		$_sql = "SELECT category_id FROM {{category}} WHERE category_name=".$cat;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	public function getStoreId($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT store_id FROM  {{goods_store}} WHERE goods_id=".$goods_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	
	/*
	 * 根据用户名查找所属商品
	 */
	public function allByUserName($type_name){
		$cmodel = new CCCModel();
		$user = User::model();
		$user_name = Yii::app()->user->name;
		$user_name = "'$user_name'";
		if($type_name == '管理员'){
			$_sql .= "SELECT * FROM {{goods}} ";
		}elseif($type_name == '商家'){
			$_sql .= "SELECT * FROM {{goods}} WHERE goods_id IN(
				SELECT goods_id FROM {{goods_store}} WHERE store_id IN(
					SELECT store_id FROM {{store}} WHERE store_owner IN(
						SELECT user_id FROM {{user}} WHERE user_name=$user_name
					)
				)
			)  ";
		}
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
/**
	 * 遍历配置参数
	 */

	public function ParamList($goods_id){
		$cmodel = new CCCModel();	
		$sql = "SELECT category_id FROM {{goods}} WHERE goods_id=".$goods_id;
		$category_id = $cmodel->sel($sql);
		$category_id = $category_id[0]['category_id'];
		$_sql = "SELECT a.param_id,a.param_name,(SELECT f.param_name FROM xy_param f WHERE a.parent_id=f.param_id) as parent_name,(SELECT b.goods_param_id FROM {{goods_param}} b WHERE   a.param_id = b.param_id and b.active = 1 and b.goods_id = $goods_id) as goods_param_id FROM {{param}} a WHERE a.parent_id <> 0 AND a.active = 1";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	
	/**
	 * 遍历配置属性
	 */

	public function AttrList($goods_id){
		$cmodel = new CCCModel();	
		$sql = "SELECt category_id FROM {{goods}} WHERE goods_id=".$goods_id;
		$category_id = $cmodel->sel($sql);
		$category_id = $category_id[0]['category_id'];
//		$_sql = "SELECT a.attr_id,a.attr_name,a.parent_id,(SELECT b.goods_attr_id FROM {{goods_attr}} b WHERE   a.attr_id = b.attr_id and b.active = 1 and b.goods_id = $goods_id) as goods_attr_id FROM {{attr}} a WHERE a.active = 1 ORDER BY attr_sort ASC";
		
		$_sql = "SELECT a.attr_id,a.attr_name,(SELECT f.attr_name FROM xy_attr f WHERE a.parent_id=f.attr_id) as parent_name,(SELECT b.goods_attr_id FROM {{goods_attr}} b WHERE   a.attr_id = b.attr_id and b.active = 1 and b.goods_id = $goods_id) as goods_attr_id FROM {{attr}} a WHERE a.parent_id <> 0 AND a.active = 1";
		
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	
	
	
	/**
	 * 为商品配置参数
	 */
	public function updParam($goods_id,$arrParam){
		$cmodel = new CCCModel();	
		$_sql = "UPDATE {{goods_param}} SET active = 0 WHERE goods_id=".$goods_id;
		$cmodel->upd($_sql);
		foreach ($arrParam as $_v){ 		
			$_sql = "SELECT * FROM {{goods_param}} WHERE goods_id=".$goods_id." AND param_id=".$_v;
			$result = $cmodel -> sel($_sql);
			if(!empty($result)){
				$_sql = "UPDATE {{goods_param}} SET active = 1 WHERE goods_id=".$goods_id." AND param_id=".$_v;
				$result = $cmodel->upd($_sql);
			}else{
				$_sql = "INSERT INTO  {{goods_param}}(`goods_id`,`param_id`,`active`) VALUES('".$goods_id."','".$_v."',1)";
				$result = $cmodel->upd($_sql);
			}
		} 			
		return $result;
	}
	
	/**
	 * 为商品配置属性
	 */
	public function updAttr($goods_id,$arrAttr){
		$cmodel = new CCCModel();	
		$_sql = "UPDATE {{goods_attr}} SET active = 0 WHERE goods_id=".$goods_id;
		$cmodel->upd($_sql);
		foreach ($arrAttr as $_v){ 		
			$_sql = "SELECT * FROM {{goods_attr}} WHERE goods_id=".$goods_id." AND attr_id=".$_v;
			$result = $cmodel -> sel($_sql);
			if(!empty($result)){
				$_sql = "UPDATE {{goods_attr}} SET active = 1 WHERE goods_id=".$goods_id." AND attr_id=".$_v;
				$result = $cmodel->upd($_sql);
			}else{
				$_sql = "INSERT INTO  {{goods_attr}}(`goods_id`,`attr_id`) VALUES('".$goods_id."','".$_v."')";
				$result = $cmodel->upd($_sql);
			}
		} 			
		return $result;
	}
	
	
	
	
	/**
	 * 查找商家所拥有的商品id
	 */
	public function getGoodsByTypeName($type_name){
		$_sql = '';
		$cmodel = new CCCModel();
		$user = User::model();
		$user_name = Yii::app()->user->name;
		$user_name = "'$user_name'";
		if($type_name == '管理员'){
			$_sql .= "SELECT goods_id,goods_name,(SELECT rec FROM {{goods_store}} b WHERE b.goods_id=a.goods_id) as rec FROM {{goods}} a";
		}elseif($type_name == '商家'){
			$_sql .= "
				SELECT goods_id,rec FROM {{goods_store}} WHERE store_id IN(
					SELECT store_id FROM {{store}} WHERE store_owner IN(
						SELECT user_id FROM {{user}} WHERE user_name=$user_name
					)
				)";
		}
		$result = $cmodel->sel($_sql);
		return $result;
	}	
	
	/**
	 * 查找商家所拥有的商品名
	 */
	
	public function getGoodsNameByTypeName($type_name){
		$_sql = '';
		$cmodel = new CCCModel();
		$user = User::model();
		$user_name = Yii::app()->user->name;
		$user_name = "'$user_name'";
		if($type_name == '管理员'){
			$_sql .= "SELECT * FROM {{goods}} ";
		}elseif($type_name == '商家'){
			$_sql .= "SELECT * FROM {{goods}} WHERE goods_id IN(
				SELECT goods_id FROM {{goods_store}} WHERE store_id IN(
					SELECT store_id FROM {{store}} WHERE store_owner IN(
						SELECT user_id FROM {{user}} WHERE user_name=$user_name
					)
				)
			)  ";
		
		}	
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 按照sort查询所有数据
	 */
	public function all($type_name){
		$cmodel = new CCCModel();
		$user = User::model();
		$user_name = Yii::app()->user->name;
		$user_name = "'$user_name'";
		if($type_name == '管理员'){
			$_sql .= "SELECT * FROM {{goods}} ";
		}elseif($type_name == '商家'){
			$_sql .= "SELECT * FROM {{goods}} WHERE goods_id IN(
				SELECT goods_id FROM {{goods_store}} WHERE store_id IN(
					SELECT store_id FROM {{store}} WHERE store_owner IN(
						SELECT user_id FROM {{user}} WHERE user_name=$user_name
					)
				)
			)  ";
		}
		
		
		
//		$_sql = "SELECT * FROM {{goods}} ";
		$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
			
			$per = 10;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per);
			$_sql .= " ORDER BY goods_sort ASC ".$page->limit;
			$result = $cmodel->sel($_sql);
			
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
		
		
		
		
		return $result;
	}
	/**
	 * 查询所有分类信息
	 */
	public function categoryAll(){
		$cmodel = new CCCModel();
		$info = $cmodel->sel("SELECT * FROM {{category}}");
		$a = array();
		foreach ($info as $_k => $_v){
			$a["$_v[category_id]"] = $_v['category_name'];
		}
		return $a;
	}
	/**
	 * 查询所有品牌信息
	 */
	public function brandAll($category_id){
		$cmodel = new CCCModel();
		$info = $cmodel->sel("SELECT * FROM {{brand}} WHERE category_id=".$category_id);
		$a = array();
		foreach ($info as $_k => $_v){
			$a["$_v[brand_id]"] = $_v['brand_name'];
		}
		return $a;
	}
	
	/**
	 * 按照sort排序
	 * @param unknown_type $_data
	 */
	public function sort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE {{goods}} SET goods_sort=".$_v." WHERE goods_id=".$_k;
			$a = $cmodel->upd($_sql);
		}
	}
}
	
?>